// pages/home/home.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

const db = wx.cloud.database()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        swiperImg: [],
        value: '',
        text: '',
        cityShow: false,
        citys: [],
        iscityload:true,
        hotelCardList: [],
        hotelCardList1: [],
        hotelCardList2: [],
        hotelCardList3: [],
        hotelCardList4: [],
        statusBarHeight:'',
        loading:true
    },
    // 搜索
    searchChange(e) {
      this.setData({
        value: e.detail,
      });
    },
    searchKey() {
      // console.log(this.data.value);
      wx.navigateTo({
        url: '../search/search?searchKey='+this.data.value,
      })
    },
    searchClick() {
      wx.navigateTo({
        url: '../search/search?searchKey='+this.data.value,
      })
    },

    // 日期
    dateDisplay() {
      wx.hideTabBar()
      this.setData({ show: true });
    },
    dateClose() {
      wx.showTabBar()
      this.setData({ show: false });
    },
    formatDate(date) {
      date = new Date(date);
      return `${date.getMonth() + 1}/${date.getDate()}`;
    },
    dateConfirm(event) {
      wx.showTabBar()
      const [start, end] = event.detail;
      this.setData({
        show: false,
        date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
      });
    },

    // 城市
    cityShowPopup() {
      wx.hideTabBar()
      this.setData({ cityShow: true });
      // 云开发初始化
      wx.cloud.init({
        traceUser: true
      })
      // 通过云函数获取城市列表
      wx.cloud.callFunction({
        name: 'getcitylists',
        complete: res => {
          // console.log(111,res.result.data);
          let city = []
          for(var i = 0; i < res.result.data.length; i++) {
            city.push(res.result.data[i].city)
          }
          this.setData({
            citys: [...city],
            iscityload: false
          })
          // console.log(333,this.data.citys);
        }
      })
    },
    cityConfirm(event) {
      const { picker, value, index } = event.detail;
      console.log(`当前值：${value}, 当前索引：${index}`);
      this.setData({
        cityShow: false
      })
      wx.showTabBar()
      wx.navigateTo({
        url: '../search/search?searchKey='+value,
      })
    },
    cityCancel() {
      wx.showTabBar()
      this.setData({ cityShow: false });
    },

    // 九宫格
    gridSearch(e) {
      // console.log(e.currentTarget.dataset.city);
      wx.navigateTo({
        url: '../search/search?searchKey='+e.currentTarget.dataset.city,
      })
    },

    // 城市列表
    async getcitylIst(city,num) {
      const _ = db.command
      let result = await db.collection('hotel_details').where(_.or([
        {
          name: db.RegExp({
            regexp: '.*' + city,
            options: 'i',
          })
        }
      ])).get().then(res => {
        // console.log(111,res.data)
        return res.data
      }).catch(err => {
        console.log(err);
      })
      if(num==0) {
        this.setData({
          hotelCardList: result,
          loading:false
        })
      } else if(num==1) {
        this.setData({
          hotelCardList1: result,
          loading:false
        })
      } else if(num==2) {
        this.setData({
          hotelCardList2: result,
          loading:false
        })
      } else if(num==3) {
        this.setData({
          hotelCardList3: result,
          loading:false
        })
      } else {
        this.setData({
          hotelCardList4: result,
          loading:false
        })
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      db.collection('swiper').where({}).get().then(res => {
        // console.log(res);
        this.setData({
          swiperImg: res.data
        })
      }).catch(err => {
        console.log(err);
      })
      this.getcitylIst('北京',0)
      this.getcitylIst('上海',1)
      this.getcitylIst('杭州',2)
      this.getcitylIst('深圳',3)
      this.getcitylIst('南京',4)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      // console.log(wx.getSystemInfoSync())
      this.setData({
        statusBarHeight:wx.getSystemInfoSync().statusBarHeight
      })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})